<?php
namespace app\admin\controller;
use think\Controller;
use think\facade\View;
use traits\controller\Jump;
use app\common\annotation\NodeAnnotation;
class Webupload extends Controller
{
    //use Jump;
    /**
     * Notes:app应用phpoto上传
     * User: chenping
     * email: 375129701@qq.com
     */
    public function uploadappphoto()
    {
        $appFileList = [];
        $files_path = DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.'photos'.DIRECTORY_SEPARATOR;
        $up_path = dirname(app_path()).DIRECTORY_SEPARATOR.'public'.$files_path;
        $files = request()->file('file');
        $info = $files->move($up_path);
        if($info)
        {
            $file = $files_path.date("Ymd",time()).DIRECTORY_SEPARATOR.$info->getFilename();
            array_push($appFileList,str_replace("\\","/",$file));
        }else{
            $this->error($files->getError());
        }
        $this->success("上传成功",'',$appFileList);
    }

    /**
     * Notes:app应用附件上传
     * User: chenping
     * email: 375129701@qq.com
     */
    public function uploadapp()
    {
        $appFileList = [];
        $files_path = DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.'packs'.DIRECTORY_SEPARATOR;
        $up_path = dirname(app_path()).DIRECTORY_SEPARATOR.'public'.$files_path;
        $files = request()->file('file');
        $info = $files->move($up_path);
        if($info)
        {
            $file = $files_path.date("Ymd",time()).DIRECTORY_SEPARATOR.$info->getFilename();
            array_push($appFileList,str_replace("\\","/",$file));
        }else{
            $this->error($files->getError());
        }
        $this->success("上传成功",'',$appFileList);
    }

    /**
     * Notes:广告封面上传
     * User: chenping
     * email: 375129701@qq.com
     */
    public function upadphoto()
    {
        $appFileList = [];
        $files_path = DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'ad'.DIRECTORY_SEPARATOR;
        $up_path = dirname(app_path()).DIRECTORY_SEPARATOR.'public'.$files_path;
        $files = request()->file('file');
        $info = $files->move($up_path);
        if($info)
        {
            $file = $files_path.date("Ymd",time()).DIRECTORY_SEPARATOR.$info->getFilename();
            array_push($appFileList,str_replace("\\","/",$file));
        }else{
            $this->error($files->getError());
        }
        $this->success("上传成功",'',$appFileList);
    }

    /**
     * Notes:上传换灯片
     * User: chenping
     * email: 375129701@qq.com
     */
    public function upslidephoto()
    {
        $appFileList = [];
        $files_path = DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'slide'.DIRECTORY_SEPARATOR;
        $up_path = dirname(app_path()).DIRECTORY_SEPARATOR.'public'.$files_path;
        $files = request()->file('file');
        $info = $files->move($up_path);
        if($info)
        {
            $file = $files_path.date("Ymd",time()).DIRECTORY_SEPARATOR.$info->getFilename();
            array_push($appFileList,str_replace("\\","/",$file));
        }else{
            $this->error($files->getError());
        }
        $this->success("上传成功",'',$appFileList);
    }


    /**
     * Notes:上传站点logo
     * User: chenping
     * email: 375129701@qq.com
     */
    public function upsitelogo()
    {
        $appFileList = [];
        $files_path = DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'site/logo'.DIRECTORY_SEPARATOR;
        $up_path = dirname(app_path()).DIRECTORY_SEPARATOR.'public'.$files_path;
        $files = request()->file('file');
        $info = $files->move($up_path);
        if($info)
        {
            $file = $files_path.date("Ymd",time()).DIRECTORY_SEPARATOR.$info->getFilename();
            array_push($appFileList,str_replace("\\","/",$file));
        }else{
            $this->error($files->getError());
        }
        $this->success("上传成功",'',$appFileList);
    }


    /**
     * Notes:广告封面上传
     * User: chenping
     * email: 375129701@qq.com
     */
    public function upuseravatar()
    {
        $appFileList = [];
        $files_path = DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'user'.DIRECTORY_SEPARATOR;
        $up_path = dirname(app_path()).DIRECTORY_SEPARATOR.'public'.$files_path;
        $files = request()->file('file');
        $info = $files->move($up_path);
        if($info)
        {
            $file = $files_path.date("Ymd",time()).DIRECTORY_SEPARATOR.$info->getFilename();
            array_push($appFileList,str_replace("\\","/",$file));
        }else{
            $this->error($files->getError());
        }
        $this->success("上传成功",'',$appFileList);
    }

    /**
     * Notes:门户应用
     * User: chenping
     * email: 375129701@qq.com
     */
    public function postupload()
    {
        $appFileList = [];
        $files_path = DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'post'.DIRECTORY_SEPARATOR;
        $up_path = dirname(app_path()).DIRECTORY_SEPARATOR.'public'.$files_path;
        $files = request()->file('file');
        $info = $files->move($up_path);
        if($info)
        {
            $file = $files_path.date("Ymd",time()).DIRECTORY_SEPARATOR.$info->getFilename();
            array_push($appFileList,str_replace("\\","/",$file));
        }else{
            $this->error($files->getError());
        }
        $this->success("上传成功",'',$appFileList);
    }

    /**
     * Notes:上传
     * User: chenping
     * email: 375129701@qq.com
     */
    public function webuploader()
    {
        if ($this->request->isPost())
        {

            $uploader = new \app\common\lib\Upload();
            $result = $uploader->upload();

            if ($result === false)
            {
                echo json_encode([
                    "code"=> 0,
                    "msg" => $uploader->getError(),
                    'data'=> []
                ]);
            } else {
                echo json_encode([
                    "code"=> 1,
                    "msg" => '上传成功',
                    'data'=> $result
                ]);
            }

        }else{
            //允许上传的文件类型
            $uploadSetting = get_upload_setting();
            $arrFileTypes = [
                'image' => ['title' => 'Image files', 'extensions' => $uploadSetting['file_types']['image']['extensions']],
                'video' => ['title' => 'Video files', 'extensions' => $uploadSetting['file_types']['video']['extensions']],
                'audio' => ['title' => 'Audio files', 'extensions' => $uploadSetting['file_types']['audio']['extensions']],
                'file'  => ['title' => 'Custom files', 'extensions'=> $uploadSetting['file_types']['file']['extensions']]
            ];

            //设置默认文上传的文件类型
            $arrData = $this->request->param();
            if (empty($arrData["filetype"]))
            {
                $arrData["filetype"] = "image";
            }

            //检索文件类型是否合法
            $fileType = $arrData["filetype"];
            if (array_key_exists($arrData["filetype"], $arrFileTypes))
            {
                $extensions                = $uploadSetting['file_types'][$arrData["filetype"]]['extensions'];
                $fileTypeUploadMaxFileSize = $uploadSetting['file_types'][$fileType]['upload_max_filesize'];
            } else {
                $this->error('上传文件类型配置错误！');
            }


            View::share('filetype', $arrData["filetype"]);
            View::share('extensions', $extensions);
            View::share('upload_max_filesize', $fileTypeUploadMaxFileSize * 1024);
            View::share('upload_max_filesize_mb', intval($fileTypeUploadMaxFileSize / 1024));
            $maxFiles  = intval($uploadSetting['max_files']);
            $maxFiles  = empty($maxFiles) ? 20 : $maxFiles;
            $chunkSize = intval($uploadSetting['chunk_size']);
            $chunkSize = empty($chunkSize) ? 512 : $chunkSize;
            View::share('max_files', $arrData["multi"] ? $maxFiles : 1);
            View::share('chunk_size', $chunkSize); //// 单位KB
            View::share('multi', $arrData["multi"]);
            View::share('app', $arrData["app"]);


            //读取上传插件机制
            $content = hook_one('fetch_upload_view');
            $tabs = ['local', 'url', 'cloud'];
            $tab = !empty($arrData['tab']) && in_array($arrData['tab'], $tabs) ? $arrData['tab'] : 'local';
            if (!empty($content))
            {
                $this->assign('has_cloud_storage', true);
            }
            if (!empty($content) && $tab == 'cloud')
            {
                return $content;
            }
            $tab = $tab == 'cloud' ? 'local' : $tab;

            $this->assign('tab', $tab);

            return $this->fetch();
        }
    }

}